package com.frxuexi.base.http

import com.blankj.utilcode.util.SPUtils
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitUtil {

    private val client by lazy {
        OkHttpClient.Builder()
            .readTimeout(10,TimeUnit.SECONDS)
            .connectTimeout(10,TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("accessId", "ac8a0750895b40b8881ace3a10b88d29")
                    .addHeader("token", SPUtils.getInstance().getString(Const.TOKEN,"token"))
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .client(client)
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private val clientShop by lazy {
        OkHttpClient.Builder()
            .readTimeout(10,TimeUnit.SECONDS)
            .connectTimeout(10,TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("Authori-zation", "27173dbbb52a410a8712886ee1123286")
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofitShop by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL_SHOP)
            .client(clientShop)
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}